package vashion.azeroth.api.controller.member;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalComment;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalCommentService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/member")
public class CommentController<T> extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
    @Autowired
    private PersonalCommentService<T> personalCommentService;

    /**
     * 添加评论
     */
    @RequestMapping("addComment.htm")
    @ResponseBody
    public ApiResult<PersonalComment> addFavorite(@ModelAttribute("data") PersonalComment personalComment,
                                                  HttpServletRequest request) {
        ApiResult<PersonalComment> result = new ApiResult<PersonalComment>();
        try {
            personalComment = PersonalComment.buildFromJson(JSON.parseObject(request.getParameter("data")));
            if (null == personalComment) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }

            if (null == personalComment.getContent()
                    || StringUtils.isBlank(String.valueOf(personalComment
                    .getContent()))) {
                return ApiResult.newErrorResult(
                        GenericErrorEnum.NOT_EMPTY.getErrorCode(), "评论内容"
                                + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
            }
            logger.error("评价内容:{}", JSON.toJSON(personalComment));
            result = personalCommentService.addComment(personalComment);

        } catch (Exception e) {
            logger.error("addFavorite exception ", e);
        }
        return result;
    }

    /**
     * 取消关注
     *
     * */

}
